2023-08-17

Introduction

In this presentation I will use “Insectsprays” data to create a plot using plotly. The data set Insectsprays gives the counts of insects in agricultural experimental units treated with different insecticides.

Loading required packages:

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data("InsectSprays")

Get the summary of the data set

summary(InsectSprays)
##      count       spray 
##  Min.   : 0.00   A:12  
##  1st Qu.: 3.00   B:12  
##  Median : 7.00   C:12  
##  Mean   : 9.50   D:12  
##  3rd Qu.:14.25   E:12  
##  Max.   :26.00   F:12

Plotting the data using plotly

trend <- aggregate(count~spray, data=InsectSprays, mean)
fit <- lm(count~spray, data= trend)
plot_ly(trend, x=~spray, y=~count, type="scatter", mode   = 'markers')%>% 
add_lines(x=~spray, y=fitted(fit))%>% 
layout(title="Relationship between the Insect Sprays and Insect Count")